import * as fetch from 'common/fetch.js';
import areaData from 'common/area.js';

/**
 * 会员购物车列表
 */
export function memberCartList({userId}) {
  return fetch.get('/member/memberCartList/'+userId);
}

/**
 * 购物车列表
 */
export function cartList({cartIds}) {
  return fetch.get('/member/cartList/'+cartIds);
}

/**
 * 添加购物车
 */
export function addCart({memberId, goodsId, goodsNum}) {
  return fetch.post('/member/addCart', {memberId, goodsId, goodsNum});
}


/**
 * 更新购物车
 */
export function updateCart({cartId, goodsNum}) {
  return fetch.post('/member/updateCart', {cartId, goodsNum});
}

/**
 * 删除购物车
 */
export function deleteCart({cartIds}) {
  return fetch.post('/member/deleteCart', {cartIds});
}

/**
 * 获取用户地址列表
 */
export function userAddressList({userId}) {
  return fetch.get('/member/addressList/'+userId);
}

/**
 * 获取用户地址详情
 */
export function userAddressDetail({addressId}) {
  return fetch.get('/member/address/'+addressId);
}

/**
 * 添加收货地址
 */
export function addAddress({memberId, receiverName, phone, provinceId, cityId, areaId, address}) {
  return fetch.post('/member/addAddress', {
  	memberId, receiverName, phone, provinceId, cityId, areaId, address
  });
}

/**
 * 更新收货地址
 */
export function updateAddress({addressId, memberId, receiverName, phone, provinceId, cityId, areaId, address}) {
  return fetch.post('/member/updateAddress', {
  	addressId, memberId, receiverName, phone, provinceId, cityId, areaId, address
  });
}

/**
 * 获取省市区
 */
export function getAreaData() {
  let convertedData = areaData.map(item => {
    let province = {
      value: item.areaId,
      label: item.areaName,
    };

    const cityList = item.children.map(city => {
      return {
        value: city.areaId,
        label: city.areaName,
        children: city.children.map(area => {
          return {
            value: area.areaId,
            label: area.areaName
          }
        })
      }
    })

    province.children = cityList
    return province;
  })

  return convertedData;
}

/**
 * 余额支付
 */
export function balancePay({orderId}) {
  return fetch.post('/pay/balance/pay/'+orderId);
}

/**
 * 余额充值
 */
export function balanceRecharge({memberId, rechargeType, rechargeMoney}) {
  return fetch.post('/pay/balance/recharge', {memberId, rechargeType, rechargeMoney});
}

/**
 * 账号登录
 */
export function login({loginAccount, loginPassword, accountType}) {
  return fetch.post('/auth/login', {loginAccount, loginPassword, accountType});
}

/**
 * 发送短信
 */
export function sendPhoneCode({phone}) {
  return fetch.post('/shop/sendPhoneCode', { phone });
}

/**
 * 手机号登录
 */
export function loginByPhone({loginPhone, loginCode}) {
  return fetch.post('/shop/user/loginByPhone', {loginPhone, loginCode});
}

/**
 * 获取用户信息
 */
export function userInfo({memberId}) {
  return fetch.get('/member/'+memberId);
}